#!/usr/bin/perl
# diskswap.cgi in diskman

require './diskman-lib.pl';

&ReadParse();

my ($index, $scsiid, $sysvol_at, $userpart, $sysvpart);

# set default value
$sysvol_at=$hwinfo{'sysvol_at'};
$userpart =$hwinfo{'userpart'};
$sysvpart =$hwinfo{'sysvpart'};
$markpart =$hwinfo{'markpart'};
if(!$sysvol_at) { $sysvol_at=0; }
if(!$sysvpart)  { $sysvpart =1; }
if(!$userpart)  { $userpart =5; }
if(!$markpart)  { $markpart =6; }


my $diskname=$in{'disk'};

#
@rdinfo=get_check_raid_info();
@dinfo=get_diskinfo(\@rdinfo);

#get disk info
my $found=0;
my %disk;

foreach $d (@dinfo) {
	if ($d->{'name'} eq $diskname) { 
		$found=1;
		%disk=%{$d};
		last;
	}
}
if(!$found) {
	$err_msg = $text{'err_cgiparam'};
	goto ERROR_OUT;
}


if($disk{'status'} eq 'well' || $disk{'status'} eq 'unknown' ) 
{

##
# REMOVE DISK
#
	if($disk{'type'} ne 'scsi') {
		$err_msg = &text('err_drm_scsi',$diskname);
		goto ERROR_OUT;
	}

	$out=`scsi_info $disk{'device'}`;
	if ( $out !~ /SCSI_ID="(\d+),(\d+),(\d+),(\d+)"/ ) {
		$err_msg = $text{'err_drm_scsiid'};
		goto ERROR_OUT;
	}
	$scsiid="$1 $2 $3 $4";
	
	#check uservol raid info
	if($disk{'using'} eq 'used') 
	{ 
		if($disk{'volname'} =~ /volume(\d)/) { $index=$1-1; }
		%uservol=%{$rdinfo[$index]};

		$err_msg=&chkuservol(\%disk,\%uservol);
		if($err_msg) {
			goto ERROR_OUT;
		}
	} 
	my ($md,$dev,$st);
	$md  = '/dev/md'.$sysvol_at;
	$dev = $disk{'device'}.$sysvpart;
	$st=&chksysvol($md,$dev);
	if( $st < 0 ) 
	{
		$err_msg =$text{'err_cgiparam'};
		$err_msg =&text('err_drm_sysv1',$disk{'volname'})  if($st==-2);
		$err_msg =&text('err_drm_resync',$disk{'volname'}) if($st==-3);
		goto ERROR_OUT;
	}

	if( $st > 0 )
	{
		`raidsetfaulty $md  $dev 2>/dev/null `;
		`raidhotremove $md  $dev 2>/dev/null `;
		if($?) {
			$err_msg = text{'err_drm_sysvrm'};
			goto ERROR_OUT;
		}
	}

	if($disk{'using'} eq 'used') 
	{
		$md 	= $disk{'raidgrp'};
		$dev    = $disk{'device'}.$userpart;
		`raidsetfaulty $md  $dev 2>/dev/null `;
		`raidhotremove $md  $dev 2>/dev/null `;
		if($?) {
			$err_msg = &text('err_drm_usrvrm',$disk{'volname'});
			goto ERROR_OUT;
		}
	}

	`./hotswap -r $scsiid >/dev/null 2>&1 `;

	if ( $? == 0 ) {
		&redirect("index.cgi");
	} else {
		$err_msg = "$scsiid";
		goto ERROR_OUT;
	}

} 
elsif( $disk{'status'} eq 'offline') 
{

	if($diskname =~ /disk(\d+)/) { 
		$index=$1-1;
	} else {
		$err_msg = $text{'err_cgiparam'};
		goto ERROR_OUT;
	}

	open(DISKINFO,$config{'diskinfo'});
	my @line=<DISKINFO>;
	close(DISKINFO);

	if($line[$index] =~ /\S+=\S+\s+(\d+),(\d+),(\d+),(\d+)\s+(\S+)$/ ) { 
		$scsiid	="$1 $2 $3 $4";
		$port	=$5;
	} else {
		$err_msg = $text{'err_cgiparam'};
		goto ERROR_OUT;
	}

	open(MDSTAT, $config{'mdstat'});
	while( <MDSTAT> ){
		if( $_ !~ /^md0 :/ ) { next; }
		else {
			$line[0]=$_;
			$line[1]=<MDSTAT>;
			$line[2]=<MDSTAT>;
			last;
		}
	}
	close(MDSTAT);

	if($line[2] !~ /^\s*$/ ) {
		$err_msg =&text('err_drm_resync',$disk{'volname'});
		goto ERROR_OUT;
	}

##
# INSTALL NEW DISK
#

	
	`./hotswap -a $scsiid >/dev/null 2>&1 `;
	if ( $? != 0 ) { goto ERROR_OUT; }

	my (@dindex,$rdev,$out,$newstatus);

	@dindex=&get_diskindex();
	$rdev=&diskname2rdev(\@dindex,$diskname);
	$newstatus=&checkformat($rdev.$markpart);
	if($newstatus){
		&update_diskinfo($index,$newstatus);
	}

	my $md="/dev/md".$sysvol_at;

	if($newstatus eq 'well' && mounton($md,'/sysvol') ){
		`raidhotadd $md $rdev$sysvpart`;
	}
	&redirect("index.cgi");
	


} else {
	$err_msg = $text{'err_cgiparam'};
	goto ERROR_OUT;
}

return;

ERROR_OUT:
&error_setup("");
&error($err_msg);


sub chkuservol
{
	my ($d,$u)=@_;

	my ($i,$dn,$dstatus);

	$dn=$d->{'device'}.$hwinfo{'userpart'};

	for($i=0;$i< @{$u->{'rdevs'}};$i++)
	{ 
		if($u->{'rdevs'}->[$i] eq $dn )
		{
			$dstatus=$u->{'rdevs_role'}->[$i];
			last;
		}
	}

	if ( $u->{'level'} eq 'raid1') 
	{
		if($dstatus eq 'good'&&($u->{'working_disks'}==1)) 
		{
			return $text{'err_drm_raid1'};
		}
	} 
	elsif ( $u->{'level'} eq 'raid5')  
	{
		if($u->{'working_disks'}!=$u->{'raid_disks'}) 
		{
			return $text{'err_drm_raid5'};
		}
	} 
	else 
	{
		return &text('err_drm_level', $d->{'name'},$d->{'volname'},
			     $u->{'level'});
	}

	if($u->{'resync'} == 1) { 
		return &text('err_drm_resync',$d->{'volname'});
	}
	return undef;
}

sub checkformat
{
	my ($disk,$MARK,$out);

	$MARK='Esteem EzStor1.0';

	$disk=$_[0];

	$out=`dd if=$disk bs=16 count=1 2>/dev/null`;

	if($?) { return 'unknown'; }

	if($out eq $MARK) {
		return 'well';
	}else{
		return 'unknown';
	}

}
